// Netcat1 is a read-only TCP client
package main

import (
	"io"
	"log"
	"net"
	"os"
)

func main() {
	conn, err := net.Dial("tcp", "localhost:8000")
	if tcp, b := conn.(*net.TCPConn); b {
		if err != nil {
			log.Fatal(err)
		}
		done := make(chan struct{})
		go func() {
			n, err := io.Copy(os.Stdout, tcp) // NOTE: ignoring errors
			log.Println("done", n, err)
			done <- struct{}{} // signal the main goroutine
		}()

		mustCopy(tcp, os.Stdin)
		err := tcp.CloseWrite()
		log.Println("close write", err)
		<-done // wait for background goroutine to finish
	}

}

func mustCopy(dst io.Writer, src io.Reader) {
	if _, err := io.Copy(dst, src); err != nil {
		log.Fatal(err)
	}
}
